<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 4/6/17
 * Time: 6:50 PM
 */

namespace common\models;
use yii\base\ErrorException;

/**
 * Class Custom
 * @package common\models
 *
 * @property CreditOrder[] $creditOrders
 * @property CreditOrder $creditOrderSelf
 * @property CreditOrder $lastCreditOrder
 * @property CreditOrder $creditOrderSpouse
 * @property CreditOrder[] $creditOrderGuarantees
 * @property DividedPaymentsOrder $dividedPaymentsOrder
 * @property SpecialCard $specialCard
 * @property GuaranteeCompany $guaranteeCompany
 * @property CarOrder $lastCarOrder
 * @property CarOrder $carOrder
 * @property FileListInfo[] $fileListInfos
 * @property FileListInfo[] $fileListInfoCard
 * @property FileListInfo[] $fileListInfoCollection
 * @property FileListInfo[] $fileListInfoUnsercuredDetails
 * @property FileListInfo[] $fileListInfoCall
 * @property ManualPics[] $manualPics
 */
class Custom extends \common\models\base\Custom
{
    /**
     * @param $orderno
     * @return null|static
     */
    public static function getByOrderno($orderno)
    {
        return static::findOne(['orderno'=>$orderno]);
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'guarantee_company_id' => '担保公司编号',
            'guarantee_company_user_id' => 'Guarantee Company User ID',
            'master_name' => '客户名',
            'orderno' => '客户订单号',
            'mobile' => '手机',
            'address' => '地址',
            'unit' => '单元',
        ];
    }

    /**
     * @param $serialize_data
     * @return int
     */
    public function checkResubmitForCarOrder($car_data)
    {
        $car_order = $this->lastCarOrder;
        if (!$car_order){
            $resubmit = CarOrder::RESUBMIT_FIRST;
        }else{
            $last_log = $car_order->lastCarOrderLog;
            if ($last_log->api_return_code!='00000'&&$last_log->api_return_code!='20001'){
                if ($last_log->api_return_code == '22001' || $last_log->api_return_code == '22002'){
                    $resubmit = CarOrder::RESUBMIT_BACK_RESUB;
                }else{
                    $resubmit = CarOrder::RESUBMIT_FIRST;
                }
            }else{
                if ($last_log){
                    $last_back_log = $last_log->lastCarOrderBankBackLog;
                    if (!$last_back_log){
                        throw new ErrorException("上次提交没退回之前不能再次提交");
                    }
                }
                if (isset($car_data['carRegNo'])&&$car_data['carRegNo']&&isset($car_data['carNo0'])&&$car_data['carNo0']){
                    $resubmit = CarOrder::RESUBMIT_ADD;
                }else{
                    $resubmit = CarOrder::RESUBMIT_BACK_RESUB;
                }
            }
        }
        return $resubmit;
    }

	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getCreditOrders()
	{
		return $this->hasMany(CreditOrder::className(), ['custom_id' => 'id']);
	}

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCarOrders()
    {
        return $this->hasMany(CarOrder::className(), ['custom_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCarOrder()
    {
//        $lastCarOrder = CarOrder::find()->where(['orderno'=>]);
        return $this->hasOne(CarOrder::className(), ['custom_id' => 'id'])->orderBy(['id'=>SORT_DESC]);
    }

    /**
     * 最新的征信
     * @return \yii\db\ActiveQuery
     */
    public function getLastCreditOrder()
    {
        return $this->hasOne(CreditOrder::className(), ['custom_id' => 'id'])->orderBy(['id' => SORT_DESC]);
    }

    /**
     * 本人的征信
     * @return \yii\db\ActiveQuery
     */
    public function getCreditOrderSelf()
    {
        return $this->hasOne(CreditOrder::className(), ['custom_id' => 'id'])->where(['relation'=>'本人']);
    }

    /**
     * 配偶的征信
     * @return \yii\db\ActiveQuery
     */
    public function getCreditOrderSpouse()
    {
        return $this->hasOne(CreditOrder::className(), ['custom_id' => 'id'])->where(['relation'=>'配偶']);
    }

    /**
     * 担保人的征信
     * @return \yii\db\ActiveQuery
     */
    public function getCreditOrderGuarantees()
    {
        return $this->hasMany(CreditOrder::className(), ['custom_id' => 'id'])->where(['relation'=>'担保人']);
    }
	
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getDividedPaymentsOrder()
	{
		return $this->hasOne(DividedPaymentsOrder::className(), ['custom_id' => 'id']);
	}
	
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getSpecialCard()
	{
		return $this->hasOne(SpecialCard::className(), ['custom_id' => 'id']);
	}

    /**
     * @return \yii\db\ActiveQuery
     */
	public function getGuaranteeCompany()
    {
        return $this->hasOne(GuaranteeCompany::className(), ['id' => 'guarantee_company_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLastCarOrder()
    {
//        $lastCarOrder = CarOrder::find()->where(['orderno'=>]);
        return $this->hasOne(CarOrder::className(), ['custom_id' => 'id'])->orderBy(['id'=>SORT_DESC]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFileListInfos()
    {
        return $this->hasMany(FileListInfo::className(), ['custom_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFileListInfoCard()
    {
        return $this->hasOne(FileListInfo::className(), ['custom_id' => 'id'])->where(['type'=>FileListInfo::TYPE_CARD]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFileListInfoCollection()
    {
        return $this->hasOne(FileListInfo::className(), ['custom_id' => 'id'])->where(['type'=>FileListInfo::TYPE_COLLECTION]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFileListInfoUnsercuredDetails()
    {
        return $this->hasOne(FileListInfo::className(), ['custom_id' => 'id'])->where(['type'=>FileListInfo::TYPE_UNSERCURED_DETAILS]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFileListInfoCall()
    {
        return $this->hasOne(FileListInfo::className(), ['custom_id' => 'id'])->where(['type'=>FileListInfo::TYPE_CALL]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getManualPics()
    {
        return $this->hasMany(ManualPics::className(), ['custom_id' => 'id']);
    }
}